<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-yellow sidebar-mini">
<section class="content-header">
    <h1>创建生产令</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" class="form-horizontal">

                    </form>
                </div>
                <div class="box-body">
                    <form id="splitProdOrdForm" class="form-horizontal bg-white">
                        <div class="col-md-6">
                            <div class="col-md-12 form-group">
                                <label class="control-label">项目信息</label>
                                <input id="proId" type="hidden" style="display: none" th:value="${proId}"/>
                            </div>
                            <div class="col-md-12 form-group col-md-4">
                                <label class="control-label"
                                       style="text-align: left;width:70px;padding-right: 0px;padding-left: 10px;">项目编号</label>
                                <label class="control-label" col-md-8 style="text-align: left;padding-left: 5px;">
                                    <span id="proCode"></span>
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label col-md-4"
                                       style="text-align: left;width:70px;padding-right: 0px;padding-left: 10px;">项目名称</label>
                                <label class="control-label col-md-8" style="text-align: left;padding-left: 5px;">
                                    <span id="proName"></span>
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label col-md-4"
                                       style="text-align: left;width:70px;padding-right: 0px;padding-left: 10px;">项目数量</label>
                                <label class="control-label col-md-8" style="text-align: left;padding-left: 5px;">
                                    <span id="proQty"></span>
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label col-md-4"
                                       style="text-align: left;width:70px;padding-right: 0px;padding-left: 10px;">交货时间</label>
                                <label class="control-label col-md-8" style="text-align: left;padding-left: 5px;">
                                    <span id="cusDeliveryDate"></span>
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label col-md-4"
                                       style="text-align: left;width:70px;padding-right: 0px;padding-left: 10px;">需方</label>
                                <label class="control-label col-md-8" style="text-align: left;padding-left: 5px;">
                                    <span id="customer"></span>
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label"
                                       style="text-align: left;width:70px;padding-right: 0px;padding-left: 10px;">设计类别</label>
                                <label class="control-label" style="text-align: left;padding-left: 5px;">
                                    <span id="designStr"></span>
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label col-md-4"
                                       style="text-align: left;width:70px;padding-right: 0px;padding-left: 10px;">安装地点</label>
                                <label class="control-label col-md-8" style="text-align: left;padding-left: 5px;">
                                    <span id="insSite"></span>
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label col-md-4"
                                       style="text-align: left;width:70px;padding-right: 0px;padding-left: 10px;">销售类型</label>
                                <label class="control-label col-md-8" style="text-align: left;padding-left: 5px;">
                                    <span id="prodTypeStr"></span>
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label col-md-4"
                                       style="text-align: left;width:70px;padding-right: 0px;padding-left: 10px;">特种设备</label>
                                <label class="control-label col-md-8" style="text-align: left;padding-left: 5px;">
                                    <span id="speEquipTypeStr"></span>
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label col-md-4"
                                       style="text-align: left;width:70px;padding-right: 0px;padding-left: 10px;">项目需求</label>
                                <label class="control-label col-md-8" style="text-align: left;padding-left: 5px;">
                                    <span id="proRequirements"></span>
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label col-md-4"
                                       style="text-align: left;width:70px;padding-right: 0px;padding-left: 10px;">制造范围</label>
                                <label class="control-label col-md-8" style="text-align: left;padding-left: 5px;">
                                    <span id="manufScope"></span>
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label col-md-4"
                                       style="text-align: left;width:70px;padding-right: 0px;padding-left: 10px;">相关文件</label>
                                <label id="lbl_files" class="control-label col-md-8"
                                       style="text-align: left;padding-left: 5px;">

                                </label>
                            </div>
                        </div>
                        <div class="col-md-6" id="create">
                            <div class="col-md-12 form-group">
                                <label class="control-label">待创建生产令</label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label"
                                       style="text-align: left;width:90px;padding-right: 0px;padding-left: 10px;">生产令编号</label>
                                <label class="control-label" style="text-align: left;padding-left: 5px;">
                                    <input name="prodNo" type="text" class="form-control" readonly="readonly"/>
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label"
                                       style="text-align: left;width:90px;padding-right: 0px;padding-left: 10px;">项目名称</label>
                                <label class="control-label" style="text-align: left;padding-left: 5px;">
                                    <input name="prodNam" type="text" class="form-control" readonly="readonly"/>
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label"
                                       style="text-align: left;width:90px;padding-right: 0px;padding-left: 10px;">数量</label>
                                <label class="control-label" style="text-align: left;padding-left: 5px;">
                                    <input name="prodNum" type="text" class="form-control" min="1" max="10"
                                           readonly="readonly"
                                           oninput="handleInput(this)" autocomplete="off"/>
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label"
                                       style="text-align: left;width:90px;padding-right: 0px;padding-left: 10px;">交货期</label>
                                <label class="control-label" style="text-align: left;padding-left: 5px;">
                                    <input name="prodDelDat" type="text" class="form-control datepicker"
                                           readonly="readonly"/>
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label"
                                       style="text-align: left;width:90px;padding-right: 0px;padding-left: 10px;">类别</label>
                                <label class="control-label" style="text-align: left;padding-left: 5px;">
                                    <input name="prodTyp" type="radio" value="1"/>设备类
                                    <input name="prodTyp" type="radio" value="2" checked="checked"/>其他
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label"
                                       style="text-align: left;width:90px;padding-right: 0px;padding-left: 10px;">是否重点</label>
                                <label class="control-label" style="text-align: left;padding-left: 5px;">
                                    <input name="prodIsKey" type="radio" value="1"/>是
                                    <input name="prodIsKey" type="radio" value="0" checked="checked"/>否
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label"
                                       style="text-align: left;width:90px;padding-right: 0px;padding-left: 10px;vertical-align: top">产品分类</label>
                                <label class="control-label" style="text-align: left;padding-left: 5px;">
                                    <input name="prodBacTyp" class="form-control" type="text" maxlength="255"/>
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="control-label"
                                       style="text-align: left;width:90px;padding-right: 0px;padding-left: 10px;vertical-align: top">备注</label>
                                <label class="control-label" style="text-align: left;padding-left: 5px;">
                                    <textarea name="prodRem" style="resize: none" class="form-control" maxlength="255"
                                              rows="4" cols="70"></textarea>
                                </label>
                            </div>
                            <div class="col-md-12 form-group">
                                <button id="btn_addTempTable" type="button" shiro:hasPermission="pro:release:commit"
                                        class="btn btn-success pull-right">创建并保存
                                </button>
                            </div>
                        </div>

                    </form>
                    <!--                    <div id="createProdDiv" class="col-md-12">-->
                    <!--                        <div class="col-md-12 form-group">-->
                    <!--                            <h3>已创建生产令</h3>-->
                    <!--                        </div>-->
                    <!--                        <table id="bootstrap-table-tempProdOrd"></table>-->
                    <!--                        <button id="btn_close" type="button" class="btn btn-default pull-right"-->
                    <!--                                style="margin-left: 15px;margin-top: 10px;">取消-->
                    <!--                        </button>-->
                    <!--                        <button id="btn-save" type="button" style="margin-top: 10px;"-->
                    <!--                                shiro:hasPermission="pro:release:commit"-->
                    <!--                                class="btn btn-info file-contract btn-file pull-right">保存-->
                    <!--                        </button>-->
                    <!--                    </div>-->
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    let tempTable, tempData = new Array(), currentProject;
    $(function () {
        createProdOrd($("#proId").val());
        /**
         * 添加到临时表中
         */
        $("#btn_addTempTable").on('click', function (e) {
            // for (let i = 0; i < tempData.length; i++) {
            //     if (tempData[i].prodNo === $("#splitProdOrdForm [name='prodNo']").val()) {
            //         js.modal.warning("尊敬的用户，您输入的生产令已存在。");
            //         return;
            //     }
            // }
            let num = 0;
            for (let i = 0; i < tempData.length; i++) {
                num = parseInt(tempData[i].prodNum) + parseInt(num);
            }
            if (parseInt($("#splitProdOrdForm [name='prodNum']").val()) > parseInt(currentProject.proQty) - parseInt(num)) {
                js.modal.warning("尊敬的用户，您输入的项目数量不可大于项目数量。");
                return;
            }
            if ($("#splitProdOrdForm [name='prodNum']").val() === '') {
                js.modal.warning("尊敬的用户，请您输入数量。");
                return;
            }
            if (parseInt($("#splitProdOrdForm [name='prodNum']").val()) === 0) {
                js.modal.warning("尊敬的用户，请您输入数量。");
                return;
            }
            let tempObj = new Object();
            let orderNo = tempData.length + 1;
            tempObj.orderNo = orderNo;
            tempObj.prodProCod = currentProject.proCode;
            tempObj.proName = currentProject.proName;
            tempObj.prodNum = $("#splitProdOrdForm [name='prodNum']").val();
            tempObj.customer = currentProject.customer;
            tempObj.prodIsKey = $("#splitProdOrdForm [name='prodIsKey']:checked").val();
            tempObj.prodTyp = $("#splitProdOrdForm [name='prodTyp']:checked").val();

            let month = eval(new Date().getMonth() + 1);
            let monthStr = '';
            if (month < 10) {
                monthStr = '0' + month;
            } else {
                monthStr = '' + month;
            }
            let day = new Date().getDate();
            let dayStr = '';
            if (day < 10) {
                dayStr = '0' + day;
            } else {
                dayStr = '' + day;
            }
            let hours = new Date().getHours();
            let hoursStr = '';
            if (hours < 10) {
                hoursStr = '0' + hours;
            } else {
                hoursStr = '' + hours;
            }
            let minutes = new Date().getMinutes();
            let minutesStr = '';
            if (minutes < 10) {
                minutesStr = '0' + minutes;
            } else {
                minutesStr = '' + minutes;
            }
            let seconds = new Date().getSeconds();
            let secondsStr = '';
            if (seconds < 10) {
                secondsStr = '0' + seconds;
            } else {
                secondsStr = '' + seconds;
            }
            tempObj.prodCreDat = new Date().getFullYear() + '-' + monthStr + '-' + dayStr + ' ' + hoursStr + ':' + minutesStr + ':' + secondsStr;
            let prodDelDat = $("#splitProdOrdForm [name='prodDelDat']").val();
            if (prodDelDat === '') {
                tempObj.prodDelDat = null;
            } else {
                tempObj.prodDelDat = $("#splitProdOrdForm [name='prodDelDat']").val();
            }
            tempObj.prodRem = $("#splitProdOrdForm [name='prodRem']").val();
            tempObj.prodBacTyp = $("#splitProdOrdForm [name='prodBacTyp']").val();
            if (tempData.length > 0 && tempData.length < 9) {
                tempObj.prodNo = $("#proCode").text() + '-0' + eval(tempData.length + 1)
                $("#splitProdOrdForm [name='prodNo']").val($("#proCode").text() + '-0' + eval(tempData.length + 2));
                if (tempData.length == 1) {
                    tempData[0].prodNo = $("#proCode").text() + '-01';
                }
            } else if (tempData.length >= 9) {
                tempObj.prodNo = $("#proCode").text() + '-' + eval(tempData.length + 1)
            } else if (tempData.length == 0) {
                tempObj.prodNo = $("#proCode").text();
                $("#splitProdOrdForm [name='prodNo']").val($("#proCode").text() + '-0' + eval(tempData.length + 2));
            }
            tempData.push(tempObj);
            js.modal.confirm('尊敬的用户，您确定要保存当前操作并关闭此页面吗？', function () {
                js.post({
                    url: ctx + 'prodOrd/saveSplit',
                    type: 'post',
                    traditional: true,            // 阻止深度序列化， 使参数可以使用数组
                    dataType: 'json',
                    contentType: "application/x-www-form-urlencoded",
                    data: {
                        proId: $("#proId").val(),
                        prodOrdStr: JSON.stringify(tempData)
                    },
                    async: true,                // 异步执行
                    success: function (result) {
                        if (result.type === web_status.SUCCESS) {
                            js.modal.success(result.msg);
                            window.opener.reLoad();
                            window.open('', '_self');
                            window.close();
                        } else {
                            js.modal.warning(result.msg);
                        }
                    },
                    error: function () {
                        js.modal.fail();
                    }
                });
            });
        });
        /**
         * 关闭当前页面
         */
        $("#btn_close").on('click', function () {
            js.modal.confirm('尊敬的用户，当前操作未保存，您确定要关闭此页面吗？', function () {
                window.opener.reLoad();
                window.open('', '_self');
                window.close();
            });
        })
        // /**
        //  * 保存项目拆分结果
        //  */
        // $("#btn-save").on('click', function () {
        //     let num = 0;
        //     for (let i = 0; i < tempData.length; i++) {
        //         num = parseInt(tempData[i].prodNum) + parseInt(num);
        //     }
        //     if (num === 0) {
        //         js.modal.warning("尊敬的用户，请您创建一个生产令。");
        //         return;
        //     }
        //     if (parseInt(currentProject.proQty) != parseInt(num)) {
        //         js.modal.warning("尊敬的用户，生产令数量与项目数量必须一致。");
        //         return;
        //     }
        //     js.modal.confirm('尊敬的用户，您确定要保存当前操作并关闭此页面吗？', function () {
        //         js.post({
        //             url: ctx + 'prodOrd/saveSplit',
        //             type: 'post',
        //             traditional: true,            // 阻止深度序列化， 使参数可以使用数组
        //             dataType: 'json',
        //             contentType: "application/x-www-form-urlencoded",
        //             data: {
        //                 proId: $("#proId").val(),
        //                 prodOrdStr: JSON.stringify(tempData)
        //             },
        //             async: true,                // 异步执行
        //             success: function (result) {
        //                 if (result.type === web_status.SUCCESS) {
        //                     js.modal.success(result.msg);
        //                     window.opener.reLoad();
        //                     window.open('', '_self');
        //                     window.close();
        //                 } else {
        //                     js.modal.warning(result.msg);
        //                 }
        //             },
        //             error: function () {
        //                 js.modal.fail();
        //             }
        //         });
        //     });
        // })
    });

    /**
     * 创建项目
     */
    function createProdOrd(proId) {
        js.post({
            url: ctx + 'prodOrd/getProInfo',
            type: 'post',
            traditional: true,            // 阻止深度序列化， 使参数可以使用数组
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded",
            data: {
                proId: proId
            },
            async: true,                // 异步执行
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    let project = result.data.project;
                    currentProject = result.data.project;
                    let files = result.data.files;
                    $("#splitProdOrdForm").fillSpan(project);
                    $("#lbl_files").empty();
                    for (let i = 0; i < files.length; i++) {
                        $("#lbl_files").append('<a href="/prodOrd/downloadProjectFile/' + files[i].id + '">' + files[i].fileName + '</a>&nbsp;&nbsp;&nbsp;&nbsp;');
                    }
                    $("#splitProdOrdForm [name='prodNo']").val(project.proCode);
                    $("#splitProdOrdForm [name='prodNam']").val(project.proName);
                    $("#splitProdOrdForm [name='prodNum']").val(project.proQty);
                    // $("#splitProdOrdForm [name='prodDelDat']").val(project.cusDeliveryDate);
                    $("#splitProdOrdForm [name='designStr']").val(project.designStr);
                    loadTempProdOrdTable(tempData);
                } else {
                    js.modal.warning(result.msg);
                }
            },
            error: function () {
                js.modal.fail();
            }
        });
    }

    /**
     * 加载项目表数据
     */
    function loadTempProdOrdTable(data) {
        $("#bootstrap-table-tempProdOrd").bootstrapTable('destroy');
        tempTable = js.table.init({
            id: 'bootstrap-table-tempProdOrd',
            data: data,
            showExport: false,
            pagination: false,
            columns: [
                {
                    title: '序号', field: 'orderNo', width: '60'
                },
                {
                    title: '生产令编号', field: 'prodNo', width: '120px',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }

                    }
                },
                {
                    title: '项目名称', field: 'proName',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }

                    }
                },
                {
                    title: '项目数量', field: 'prodNum', width: '90px',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '需方', field: 'customer', width: '240px',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }

                    }
                },
                {
                    title: '是否<br>重点', field: "prodIsKey", width: '60px',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            if (parseInt(value) === 1) {
                                return '是';
                            } else {
                                return '否';
                            }
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '类别', field: "prodTyp", width: '60px',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            if (parseInt(value) === 1) {
                                return '设备类';
                            } else if (parseInt(value) === 2) {
                                return '其他';
                            }
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '产品分类', field: "prodBacTyp", width: '130px',
                },
                {
                    title: '交货期', field: 'prodDelDat', width: '90px',
                },
                {
                    title: '创建时间', field: 'prodCreDat', width: '90px',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value.substring(0, value.indexOf(' '));
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '操作', width: '60px',
                    formatter: function (value, row, index) {
                        let elements = new Array();
                        elements.push('<button class="btn btn-danger btn-xs" onclick="removeTempData(' + row.orderNo + ')">删除</button>');
                        return elements.join('');
                    }
                }
            ]
        });
    }

    /**
     * 输入检测
     * @param obj
     */
    function handleInput(obj) {
        $(obj).val($(obj).val().replace('.', '').replace('-', ''));
        if ($(obj).val().indexOf('0') === 0) {
            $(obj).val($(obj).val().substring(1));
        }
    }

    /**
     * 删除项目
     * @param orderNo
     */
    function removeTempData(orderNo) {
        let newTempData = new Array();
        let order = 0;
        for (let i = 0; i < tempData.length; i++) {
            if (parseInt(tempData[i].orderNo) != parseInt(orderNo)) {
                tempData[i].orderNo = order + 1;
                newTempData.push(tempData[i])
                order++;
            }
        }
        tempData = newTempData;
        loadTempProdOrdTable(tempData)
        $("#create").show();
    }

</script>
</body>
</html>